package com.paisheng.justlife.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.paisheng.justlife.R;
import com.paisheng.justlife.adapter.GvMenuAdapter;
import com.paisheng.justlife.bean.Waste;
import com.paisheng.justlife.common.MenuType;
import com.paisheng.justlife.common.WasteMenu;
import com.paisheng.justlife.utils.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class PickupTypeActivity extends Activity implements View.OnClickListener {

    private CheckBox cb_all;
    private GridView gv_menu;
    private List<Waste> list = WasteMenu.getMentList();
    private GvMenuAdapter menuAdapter;
    private TextView tvBack, tvTime;
    Timer timer = new Timer();
    private int recLen = 60;
    private Button btn_next;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pickup_type);
        initview();
        ClickListenerView();
        tvTime.setText(recLen + "");

    }

    @Override
    protected void onResume() {
        super.onResume();
        timer.schedule(task, 1000, 1000);
    }


    private void ClickListenerView() {
        cb_all.setOnClickListener(this);
        tvBack.setOnClickListener(this);
        tvTime.setOnClickListener(this);
        btn_next.setOnClickListener(this);
        gv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            boolean flag = true;

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Waste waste = list.get(position);
                if (waste.isChecked()) {
                    waste.setChecked(false);
                    cb_all.setChecked(false);
                } else {
                    waste.setChecked(true);
                }
                for (Waste waste1 : list) {
                    if (!waste1.isChecked()) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    cb_all.setChecked(true);

                }
                flag = true;
                menuAdapter.notifyDataSetChanged();

            }
        });

    }


    private void initview() {
        cb_all = (CheckBox) findViewById(R.id.cb_all);
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvTime = (TextView) findViewById(R.id.tv_time);
        btn_next = (Button) findViewById(R.id.btn_next);
        gv_menu = findViewById(R.id.gv_menu);
        menuAdapter = new GvMenuAdapter(PickupTypeActivity.this, list, MenuType.RECYCLE);
        gv_menu.setAdapter(menuAdapter);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cb_all:
                if (cb_all.isChecked()) {
                    for (int i = 0; i < list.size(); i++) {
                        Waste waste = list.get(i);
                        waste.setChecked(true);
                        list.set(i, waste);
                    }

                } else {
                    for (int i = 0; i < list.size(); i++) {
                        Waste waste = list.get(i);
                        waste.setChecked(false);
                        list.set(i, waste);
                    }
                }
                menuAdapter.notifyDataSetChanged();

                break;
            case R.id.tv_back:
                startActivity(new Intent(this, MainActivity.class));

                break;
            case R.id.btn_next:
                List<Waste> listWaste = new ArrayList<>();
                for (Waste waste : list) {
                    if(waste.isChecked()){
                        listWaste.add(waste);
                    }
                }
                Intent intent = new Intent(this, ConfirmPaymentActivity.class);
                intent.putExtra("listWaste", (Serializable) listWaste);
                startActivity(intent);

                break;
        }
    }

    public TimerTask task = new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    recLen--;
                    tvTime.setText("" + recLen);
                    if (recLen <= 0) {
                        startActivity(new Intent(PickupTypeActivity.this, MainActivity.class));
                        timer.cancel();
                    }

                }
            });

        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        timer.cancel();
        ToastUtil.cancelToast();
        finish();
    }
}
